import * as api from "../api";
import data from "bootstrap/js/src/dom/data";

// todo Main Search функция для получение машин
export const actToken = async (context, data) => {
  console.log(data);
  data = await api.token(data.phone, data.code);
  localStorage.token = data.response;
  console.log(localStorage.token);
  context.commit("mutToken", data.response);
};

export const actEvents = async (context) => {
  data = await api.events();
  context.commit("mutEvents", data.response);
};

export const actCheckToken = async (context) => {
  let token = localStorage.token;
  console.log("actCheckToken", token);
  if (token === null){
    context.commit("mutToken", false)
  } else {
    context.commit("mutToken", token)
  }
};

export const actUserInfo = async (context, token) => {
  data = await api.userInfo(token);
  localStorage.user = data.response;
  context.commit("mutUser", data.response);
};

export const actUserEvents = async (context, token) => {
  data = await api.userEvents(token);
  localStorage.userEvents = data.response;
  context.commit("mutUserEvents", data.response);
};

export const actUserEdit = async (context, data) => {
  data = await api.editUserInfo(data);
  localStorage.user = data.response;
  context.commit("mutUser", data.response);
};


async function pageCheck(context, mesError, mut_f, api_f, params) {
  context.commit("mutPageLoaded", false);
  context.commit("mutPageError", "");
  let data;
  if (params === undefined) {
    data = await api_f();
  } else {
    data = await api_f(params);
  }
  if (data.status == "ok" && data.response.length > 0) {
    context.commit(mut_f, data.response);
  } else {
    context.commit("mutPageError", mesError);
  }
  context.commit("mutPageLoaded", true);
}
